var apiHost = wx.getStorageSync("apiHost");    //请求地址
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payMethod: '',        //支付方式
    protocolSelect : '',
    collageType: 'common' //拼团类型
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },
  /**
   * radio 选择方式
   */
  radioChange: function (e) {
    var that = this;
    var flag = e.currentTarget.dataset.toggle;

    that.setData({
      payMethod: flag
    });
  },
  /**
   * 协议是否选中
   */
  protocolSelect : function(e){
    var that = this;
    var flag = e.currentTarget.dataset.toggle;
    var protocolSelect = '';

    if (flag==''){
      protocolSelect = "agree";
    }else{
      protocolSelect = '';
    }

    that.setData({
      protocolSelect: protocolSelect
    });
  },
  /**
   * 
   */
  goProtocol:function(){
    // wx.showLoading({
    //   title: '跳转中',
    //   icon: 'loading',
    //   mask: true
    // });
    // wx.navigateTo({
    //   url: '../firstOrdinaryCollage/firstOrdinaryCollage',
    //   success: function () {
    //     wx.hideLoading();   //隐藏加载loading
    //   },
    // });
  },

  /**
   * 确定发布拼团（校验）
   */
  checkCollage : function(){
    var that = this;

    var collagePriceVo = [];    //成团需要的价格和人数
    var collagePriceStr = '';   //成团需要的价格和人数拼接字符串

    var userId = wx.getStorageSync('userId');                          //用户id
    // var userId = '41896a7744944b6bba6da2a0e0671c3f'                       //用户id

    var collageId = wx.getStorageSync('collageId');                       //拼团id
    var originalPrice = wx.getStorageSync('originalPrice');     //原价
    var deliveryMethod = wx.getStorageSync('deliveryMethod');             //配送方式
    var maxBugCount = parseInt(wx.getStorageSync('maxBugCount'));         //一人最多购买次数
    var totalInventorys = parseInt( wx.getStorageSync('totalIntentory')); //总库存

    var collagePrice = wx.getStorageSync('collagePrice');       //拼团价格
    var peopleCount = parseInt(wx.getStorageSync('peopleCount'));         //拼团人数

    var collageType = that.data.collageType;                              //拼团类型
    var payMethod = that.data.payMethod;                                  //支付方式 
    var protocolSelect = that.data.protocolSelect;                        //是否同意协议

    var showCollageTimeFlag = wx.getStorageSync('showCollageTime');       //成团时间名称
    var ShowCollageTime = that.filterShowCollageTime(showCollageTimeFlag);//成团时间（过滤天、小时、分钟）

    var carrageId = wx.getStorageSync('carrageId');                       //运费模板Id

    
    //拼接成团参数
    collagePriceStr = '{ "peopleCount":' + peopleCount + ',"collagePrice":' + collagePrice + ' }';
    collagePriceVo.push(JSON.parse(collagePriceStr));

    if (payMethod==""){
      that.promptMsg("请选择支付方式");
      return;
    } else if (protocolSelect==""){
      that.promptMsg("请阅读同意《群拼服务协议》");
      return;
    }

    //发布拼团
    that.confirmPublishCollage(userId, collageId, originalPrice, ShowCollageTime, deliveryMethod, payMethod, maxBugCount, totalInventorys, collageType, collagePriceVo, carrageId)

  },

  /**
   * 确定发布拼团
   */
  confirmPublishCollage: function (userId, collageId, originalPrice, ShowCollageTime, deliveryMethod, payMethod, maxBugCount, totalInventorys, collageType, collagePriceVo, carrageId){
    var that = this;
    console.log(originalPrice)
    console.log(collagePriceVo)
    
    wx.showLoading({
      title: '玩命加载中',
      icon: 'loading',
      mask: true
    })

    wx.request({
      url: apiHost + 'hpp/collage/submit',
      method: 'POST',
      header: {
        "content-type": "application/json"
      },
      data: {
        userId: userId,
        collageId: collageId,
        originalPrice: originalPrice,
        collageTime: ShowCollageTime,
        deliveryMethod: deliveryMethod,
        payMethod: payMethod,
        maxBugCount: maxBugCount,
        totalInventorys: totalInventorys,
        collageType: collageType,
        collagePriceVo: collagePriceVo,
        carrageId: carrageId
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if ("10000" == code) {
          that.clearCollageStorageSync(); //清空本地缓存
          that.goCollageDetail(collageId);         //跳转详情
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: true
          });
        }
        wx.hideLoading();
      },
      fail: function (e) {
        wx.hideLoading();
        wx.showToast({
          title: "网络异常",
          icon: 'none',
          duration: 2000,
          mask: true
        });
      }
    })
  },

  /**
   * 提示信息
   */
  promptMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },

  /**
   * 过滤成团时间
   */
  filterShowCollageTime: function (collageTime){
    var ShowCollageTime = '';
    var timeFlag = [];
    var CollageTimeFlag = 0;
    if (collageTime.indexOf(" 天 ") >= 0) {
      timeFlag = collageTime.split(" 天 ");
      ShowCollageTime = parseInt(timeFlag[0])*24*60;
      collageTime = timeFlag[1];
      CollageTimeFlag = ShowCollageTime;
    } 
    if (collageTime.indexOf(" 小时 ") >= 0) {
      timeFlag = collageTime.split(" 小时 ");
      ShowCollageTime = parseInt(timeFlag[0]) * 60 ;
      collageTime = timeFlag[1];
      CollageTimeFlag = ShowCollageTime + CollageTimeFlag;
    } 
    if (collageTime.indexOf(" 分钟 ") >= 0) {
      timeFlag = collageTime.split(" 分钟 ");
      ShowCollageTime = parseInt(timeFlag[0]);
      CollageTimeFlag = ShowCollageTime + CollageTimeFlag;
    } 
    return parseInt(CollageTimeFlag);
  },

  /**
   * 返回上一步
   */
  theLastStep: function (){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.reLaunch({
      url: '../secondOrdinaryCollage/secondOrdinaryCollage',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
 * 跳转拼团详情
 */
  goCollageDetail: function (collageId) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.reLaunch({
      url: '../../collageDetail/collageDetail?collageId=' + collageId,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 清空 本地缓存
   */
  clearCollageStorageSync : function(){
    wx.removeStorageSync('collageId');
    wx.removeStorageSync('originalPrice');
    wx.removeStorageSync('deliveryMethod');
    wx.removeStorageSync('maxBugCount');
    wx.removeStorageSync('totalIntentory');
    wx.removeStorageSync('collagePrice');
    wx.removeStorageSync('peopleCount');
    wx.removeStorageSync('showCollageTime');
    wx.removeStorageSync('carrageId');
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})